/**
 * Created by weng on 2017/8/16.
 */

var data_temp = {};
var  temp_page_size= 15;

/**
 * 获取taccountApp列表
 * @param info
 */
var getNoticeInfo = function () {
    console.log("获取信息");
    var obj = {};

    $.ajax({
        url:'/hospital/getNoticeInfo',
        type:'post',
        dataType:'json',
        data:obj,
        success:function(data) {
            console.log("data",data);
            if (data.code != 200) {
                alert("查询错误");
                return;
            } else {
                if(data.info.length > 0){
                    showTableInfo( data.info,"commOrderList");
                }

            }
        }
    });
};


function showTableInfo(data_arr,id){
    var html = "";
    var thead = $('#'+id + ' thead');
    var tbody = $('#'+id + ' tbody');

    tbody.empty();
    thead.empty();
    var htmlHead = "<tr>"
        + "<th>通知人号码：</th>"
        + "<th>每日通知时间：</th>"
        + "<th>是否通知：</th>"
        + "<th>操作：</th>"

        + "</tr>"
    htmlHead = $(htmlHead);
    thead.append(htmlHead);

    for(var i in data_arr){

        html += '<tr>' +
            '<th  id="phone'+ data_arr[i].id +'">'+ data_arr[i].phone + '</th>' +
            '<th  id="password'+ data_arr[i].id +'">'+  data_arr[i].hour + '点 </th>'+
            '<th  id="flag'+ data_arr[i].id +'">'+  (data_arr[i].flag ==1?"是" :"否") + ' </th>';
        html += "<th>  "
            + "<a onclick='delnotice("+data_arr[i].flag+")'>" + (data_arr[i].flag ==1?"取消每日通知" :"开启每日通知")  + "</a>  </br>"
            + "<a onclick='updateNotice("+  JSON.stringify({flag:data_arr[i].flag,type:"phone"}) +")'>" + ("修改通知号码")  + "</a>  </br>"
            + "<a onclick='updateNotice("+  JSON.stringify({flag:data_arr[i].flag,type:"hour"}) +")'>" + ("修改通知时间")  + "</a>  </br>"
        html += '</th>';
        html += '</tr>';
    }
    var tr = $(html);
    tbody.append(tr);
}

var updateNotice = function (info) {
    layer.prompt({
        formType:0,
        value:'',
        title:'填写需要修改的号码',
    },function(value,index){
        var obj = {};
        obj.value = value;
        if(typeof  info == 'string'){
            info = JSON.parse(info);
        }
        if( info.type == 'hour' && (value < 0 || value > 23)){

            layer.msg("请填写0~23范围的时间" );
            layer.close(index);
            return;
        }
        if( info.type == 'phone' && value.length != 11){
            layer.msg("请填写正确的电话号码" );
            layer.close(index);
            return;
        }



        obj.type = info.type;
        obj.flag = info.flag;

        var url="/notice/updateNotice";
        $.ajax({
            type:'POST',
            url:url,
            data:obj,
            async:false,
            success:function(data){
                layer.close(index);
                if (data.code != 200) {
                    layer.msg("修改失败" );
                } else {
                    layer.msg('修改成功', {
                        //icon: 1,
                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        location.reload();
                    });
                }
            }
        });

    });
}


var delnotice = function (id) {
    layer.msg('你确定修改每日通知状态？', {
        time: 0 //不自动关闭
        ,btn: ['确定', '取消']
        ,yes: function(index){
            var obj = {};
            if(id==1){
                obj.value = 0;
            }else {
                obj.value = 1;
            }
            obj.flag = id;
            obj.type = 'flag';
            var url="/notice/updateNotice";
            $.ajax({
                url:url,
                type:'post',
                dataType:'json',
                data:obj,
                success:function(data){
                    layer.close(index);
                    if(200 === data.code) {
                        layer.msg('修改成功', {
                            icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            location.reload();
                        });
                    }else {
                        layer.msg(data.info );
                    }
                }
            });
        }
    });
}